/**
 * @param {number[]} parents
 * @return {number}
 */
 var countHighestScoreNodes = function(parents) {
  const nodes = parents.map(p => ({ val: 1 }));
  parents.slice(1, parents.length).forEach((p, i) => {
      if(nodes[p].left) {
          nodes[p].right = nodes[i + 1];
      } else {
          nodes[p].left = nodes[i + 1];
      }
  });
  const calc = (node) => {
      if(node.left) {
          node.val += calc(node.left);
      }
      if(node.right) {
          node.val += calc(node.right);
      }
      return node.val;
  };
  calc(nodes[0]);
  let max = 0;
  let cnt = 0;
  const traverse = (node) => {
      if(!node) {
          return;
      }
      let val = 1;
      if(node.left) {
          val *= node.left.val;
      }
      if(node.right) {
          val *= node.right.val;
      }
      if(nodes[0].val - node.val > 0) {
          val *= (nodes[0].val - node.val);
      }
      // console.log(val);
      if(val === max) {
          cnt ++;
      } else if (val > max) {
          cnt = 1;
          max = val;
      }
      traverse(node.left);
      traverse(node.right);
  }
  traverse(nodes[0]);
  return cnt;
};